import { defineStore } from 'pinia';
import router from "@/router";

export const useUserStore = defineStore({
  id: 'user',
  state: () => ({
    s_token: '',
    s_userinfo: null,
    s_modelType: '',
  }),
  getters: {
    token(state) {
        return state.s_token;
    },
    userinfo(state) {
        return state.s_userinfo;
    },
    modelType(state) {
      return state.s_modelType;
    }
  },
  actions: {
    // 更新状态
    setToken(data) {
        this.s_token = data;
    },
    setUserinfo(data) {
        this.s_userinfo = data;
    },
    // 更新模型类型
    setModelType(data) {
      this.s_modelType = data;
    },

    // 退出登录
    logout() {
        this.s_token = '';
        this.s_userinfo = '';
        router.replace({
            path: '/login'
        })
    }
  },
  //持久化储存
  persist: true
})